package com.myapi.server;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.myapi.client.LoginService;
import com.myapi.client.DAO.LoginDAO;

public class LoginServiceImpl extends RemoteServiceServlet implements
    LoginService {

  public LoginDAO login(String requestUri) {
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    LoginDAO loginDAO = new LoginDAO();

    if (user != null) {
      loginDAO.setLoggedIn(true);
      loginDAO.setEmailAddress(user.getEmail());
      loginDAO.setNickname(user.getNickname());
      loginDAO.setLogoutUrl(userService.createLogoutURL(requestUri));
    } else {
      loginDAO.setLoggedIn(false);
      loginDAO.setLoginUrl(userService.createLoginURL(requestUri));
    }
    return loginDAO;
  }

}
